package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yf1706.bean.EmpEducationHistory;
import com.yf1706.service.EmpService;
import com.yf1706.tools.UserTools;

@Controller
@RequestMapping("empEducationHistory")
public class EmpEducationHistoryController {
	@Autowired
	private UserTools ut;
	@Autowired
	private EmpService es;

	// 根据员工编号新增教育履历记录
	@RequestMapping("addEmpEducationHistory")
	public void addEmpEducationHistory(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		Map map = new HashMap<>();
		try {
			out = response.getWriter();
			System.out.println("进入addEmpEducationHistory");

			int empId = Integer.valueOf(request.getParameter("empId"));
			String schoolName = request.getParameter("schoolName");
			String education = request.getParameter("education");
			String major = request.getParameter("major");
			String contactTel = request.getParameter("contactTel");
			String contactPerson = request.getParameter("contactPerson");
			String address = request.getParameter("address");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date endDate = sdf.parse(request.getParameter("endDate"));
			Date beginDate = sdf.parse(request.getParameter("beginDate"));
			String remark = request.getParameter("remark");

			EmpEducationHistory eeh = new EmpEducationHistory(address, beginDate, contactPerson, contactTel, education, endDate, major, remark, schoolName);
			es.addEmpEducationHistoryByEmpId(empId, eeh);
			System.out.println(eeh);
			map.put("msg", "添加成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				map.put("success", false);
				map.put("msg", "日期格式不对");
				String msg = JSON.toJSONString(map);
				response.setCharacterEncoding("utf-8");
				response.getWriter().print(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// 根据编号删除教育履历记录
	@RequestMapping("deleteEmpEducationHistoryById")
	public void deleteEmpEducationHistoryById(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		Map map = new HashMap<>();
		try {
			out = response.getWriter();
			System.out.println("进入deleteEmpEducationHistoryById");
			System.out.println("id_arr" + request.getParameter("id_arr"));
			JSONArray arr = JSONArray.parseArray(request.getParameter("id_arr"));
			for (Object obj : arr) {
				int id = (int) obj;
				es.deleteEmpEducationHistoryByEmpId(id);
			}
			map.put("success", true);
			map.put("msg", "删除成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 修改对应编号的教育履历记录
	@RequestMapping("updateEmpEducationHistoryById")
	public void updateEmpEducationHistoryById(HttpServletRequest request, HttpServletResponse response) {
		Map map = new HashMap<>();
		PrintWriter out;
		try {
			out = response.getWriter();

			System.out.println("进入updateEmpEducationHistoryById");

			int empId = Integer.valueOf(request.getParameter("empId"));
			int id = Integer.valueOf(request.getParameter("id"));
			String schoolName = request.getParameter("schoolName");
			String education = request.getParameter("education");
			String major = request.getParameter("major");
			String contactTel = request.getParameter("contactTel");
			String contactPerson = request.getParameter("contactPerson");
			String address = request.getParameter("address");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = sdf.parse(request.getParameter("beginDate"));
			Date endDate = sdf.parse(request.getParameter("endDate"));
			String remark = request.getParameter("remark");

			EmpEducationHistory eeh = new EmpEducationHistory(address, beginDate, contactPerson, contactTel, education, endDate, major, remark, schoolName);
			es.addEmpEducationHistoryByEmpId(empId, eeh);
			eeh.setId(id);
			System.out.println(eeh);
			es.updateEmpEducationHistoryById(empId, eeh);

			map.put("success", true);
			map.put("msg", "编辑成功");
			String msg = JSON.toJSONString(map);
			response.setCharacterEncoding("utf-8");
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				map.put("success", false);
				map.put("msg", "日期格式不对");
				String msg = JSON.toJSONString(map);
				response.setCharacterEncoding("utf-8");
				response.getWriter().print(msg);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
